package com.easylinkin.linkappapi.openapi.util;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.easylinkin.linkappapi.openapi.dto.DeviceRegisterDTO;
import com.easylinkin.linkappapi.openapi.dto.PositionDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.repackaged.com.google.common.base.Objects;

import net.bytebuddy.asm.Advice.Enter;

public class EncryptionUtil {

	/**
	 * 
	 * @param source
	 * @return
	 */
	public static String getArrayToSHA256(byte[] bytes) {
		String encodeStr = "";
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
			messageDigest.update(bytes);
			encodeStr = byte2Hex(messageDigest.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return encodeStr;
	}

	/**
	 * byte[]转为16进制
	 *
	 * @param bytes
	 * @return
	 */
	private static String byte2Hex(byte[] bytes) {
		StringBuffer stringBuffer = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			String temp = Integer.toHexString(bytes[i] & 0xFF);
			if (temp.length() == 1) {
				stringBuffer.append("0");
			}
			stringBuffer.append(temp);
		}
		return stringBuffer.toString();
	}

	public static void main(String[] args) throws IOException {

		StringBuffer sb = new StringBuffer();

		PositionDTO one = new PositionDTO();
		one.setPositionName("大门");
		one.setPositionCode("017");


		DeviceRegisterDTO dto = new DeviceRegisterDTO();

		dto.setDeviceCategory("aa");
		dto.setDeviceNo("yxk002");
		dto.setDeviceName("bb");
		dto.setPositionType("017015");
		
		String url = String.format("appid=%s&data=%s&nonce=%s&appsecret=%s", "1632e8d51fa45fde7d54c2aa2a142f36",
				JSONObject.toJSONString(dto), "89808979708980a", "81e9b8d77b194165b985f86eb6445f13");
		
		System.err.println(url);

		// String abc="{\"positionName\":\"大门\"}";
		sb.append("appid=1632e8d51fa45fde7d54c2aa2a142f36");
		sb.append("&data=" + JSONObject.toJSONString(dto));
		// sb.append("&data="+abc);
		sb.append("&nonce=18748394739274aa932745c6a");
		sb.append("&appsecret=81e9b8d77b194165b985f86eb6445f13");

		System.err.println(sb.toString());

		Map<String, Object> map = new HashMap<>();

		String arrayToSHA256 = getArrayToSHA256(url.toLowerCase().getBytes(StandardCharsets.UTF_8));

		// sb.append("sign");
		map.put("data", JSONObject.toJSONString(dto));
		map.put("appid", "1632e8d51fa45fde7d54c2aa2a142f36");
		map.put("nonce", "89808979708980a");
		map.put("sign", arrayToSHA256);

		String doPost = HttpClientUtil.doPost("http://111.47.11.123:9051/api/device/register/environment",
				JSONObject.toJSONString(map));

		System.err.println(doPost);
		System.err.println(arrayToSHA256);
	}
}
